package com.qf.leadnewscommon.exceptions;

import com.qf.leadnewsmodel.dtos.ResponseResult;
import com.qf.leadnewsmodel.enums.AppHttpCodeEnum;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 所有非CustomException都由这个处理器处理（系统异常）
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult exception(Exception ex){
        ex.printStackTrace();
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /**
     * 所有自定义异常由这个处理器处理（业务异常）
     * @param customException
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public ResponseResult customException(CustomException customException){
        customException.printStackTrace();

        return ResponseResult.errorResult(customException.getHttpCodeEnum());
    }
}
